import cryptoJs from '../utils/encryptDES.js'
export const request = (params = {}) => {
	//二次封装 uni.request
	return new Promise((resolve, reject) => {
		//请求开始之前 显示loading
		// uni.showLoading({
		// 	title: '加载中....',
		// })
		console.log(params.params, "params.params");
		let data = {}
		switch (params.type) {
			case "apiMixUrlKeyApi":
				data = params.params
				break;
			case "drawUrl":
				data = params.params
				break;
			default:
				data = cryptoJs.encrypt(JSON.stringify(params.params), params.c)
				break;
		}
		uni.request({
			url: params.url,
			data: data || '',
			header: {
				'content-type': 'application/json'
			},
			// timeout: 50000,
			method: params.method || 'GET',
			success: (res) => {
				console.log(res, "==res=");
				if (res.data.code == 400 || res.data.code == 500) {
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 3000
					})
					reject(params.params.param.content)
				} else if (res.data.code == 506) {
					uni.setStorageSync("ban", true);
				} else {
					if (res.statusCode == 404) {
						uni.showToast({
							title: '请求失败',
							icon: 'error',
							duration: 2000
						})
					}
				}
				let data = {}
				switch (params.type) {
					case "apiMixUrlKeyApi":
						data = res
						break;
					case "sslwxbdApi":
						data = cryptoJs.decrypt(res.data.data, params.c)
						data = JSON.parse(data)
						break;
					case "drawUrl":
						data = res.data
						break;
					case "bsqUrl":
						data = cryptoJs.decrypt(res.data.data, params.c)
						data = JSON.parse(data)
						break;
					case "wycUrl":
						data = res
						break;
					default:
						data = cryptoJs.decrypt(res.data, params.c)
						data = JSON.parse(data)
						break;
				}
				resolve(data)
			},
			fail: (err) => {
				uni.showToast({
					title: '失败请重试!',
					icon: 'error',
					duration: 2000
				});
				reject(err)
			},
			complete: () => {
				if (params.c != 1008) {
					if (params.c != 1007) {
						uni.hideLoading()
					}
				}

				uni.stopPullDownRefresh()
			},
		})
	})
}